package main

import (
	"fmt"
	"regexp"
)

func main(){
	matchDigital()
	matchEnglish()
	matchEmail()
	matchCellphoneNumber()
	matchChinaID()
	matchSubmatch()
}

func matchDigital(){
	fmt.Println("----------------- matchDigital -----------------")
	var s string="32"
	b,er:=regexp.MatchString("^[0-9]+$",s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("string match: ",b)
	}
}

//match Chinese
/*
if m, _ := regexp.MatchString("^\\p{Han}+$", r.Form.Get("realname")); !m {
    return false
}
*/

func matchEnglish(){
	fmt.Println("----------------- matchEnglish -----------------")
	var s string="hello"
	b,er:=regexp.MatchString("^[a-zA-Z]+$",s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("English match: ",b)
	}
}

func matchEmail(){
	fmt.Println("----------------- matchEmail -----------------")
	var s string="shawnplaying@sina.com"
	b,er:=regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`,s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("Email match: ",b)
	}
}

func matchCellphoneNumber(){
	fmt.Println("----------------- matchCellphoneNumber -----------------")
	var s string="18910861950"
	b,er:=regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`,s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("Cell phone match: ",b)
	}
}

func matchChinaID(){
	fmt.Println("----------------- matchChinaID -----------------")
	var s string="10123319800101130x"
	b,er:=regexp.MatchString(`^(\d{15})$`,s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("15bit ID match: ",b)
	}
	b,er=regexp.MatchString(`^(\d{17})([0-9]|x)$`,s)
	if er!=nil {
		fmt.Println("error: ",er)
	}else{
		fmt.Println("18bit ID match: ",b)
	}
}

func matchSubmatch(){
	fmt.Println("----------------- matchSubMatch -----------------")
	s:="EBG_TST.properties"
	r,err:=regexp.Compile(`([A-Z]{3,5})_([A-Z]{3}).properties`)
	if err!=nil {
		fmt.Println("error of regexp.Compile()")
		return
	}
	ss:=r.FindStringSubmatch(s)
	for i,v:=range ss {
		fmt.Println("Submatch",i,"=",v)
	}
}


